package com.zxp.springboot.hello.controller;

import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxp.springboot.hello.mapper.EmployeeMapper;
import com.zxp.springboot.hello.model.Employee;
import com.zxp.springboot.hello.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.common.ExampleMapper;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author zxp
 * @date 2021-08-26 9:46
 */
@RestController
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @GetMapping("/getEmployee")
    public Employee getOne(Long id) {
        return employeeService.getById(id);
    }

    @GetMapping("/findEmployee")
    public List<Employee> findEmploye(String lastName, String email) {
        Employee employe = new Employee();
        employe.setEmail(email);
        employe.setLastName(lastName);
        return employeeMapper.findLike(employe);
    }

    @GetMapping("/employee/find")
    public List<Employee> findEmployeByEmample(String lastName, String email,@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "2") Integer rows) {

        PageHelper.startPage(1, 2);
        Example example = new Example(Employee.class,false);
        Example.Criteria criteria = example.createCriteria();

        criteria.andLike("lastName","%"+lastName+"%");
        criteria.andEqualTo("email", email);
        List<Employee> employees = employeeMapper.selectByExample(example);

        Employee employe = new Employee();
        employe.setEmail(email);
        employe.setLastName(lastName);
        // employeeMapper.findLike(employe);
        return employees;
    }

    @GetMapping("/employee/find2")
    public PageInfo<Employee> findEmployeByEmample2(String lastName, String email,@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "2") Integer rows) {

        PageHelper.startPage(page, rows);
        Example example = new Example(Employee.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andLike("lastName","%"+lastName+"%");
        criteria.andEqualTo("email", email);
        List<Employee> employees = employeeMapper.selectByExample(example);

        return new PageInfo<>(employees);
    }

    @GetMapping("/employee/page")
    public PageInfo<Employee> page(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "2") Integer rows) {

        PageHelper.startPage(page, rows);
        List<Employee> employees = employeeMapper.selectAll();
        return new PageInfo<>(employees);
    }

    @PostMapping("/save")
    public Employee insertEmployee(@RequestBody Employee employee) {
        int insert = employeeMapper.insert(employee);
        return employee;
    }

    @PostMapping("/saveBatch")
    public Integer insertEmployeeBatch() {
        int rows = 10;
        for (int i = 0; i < 10; i++) {
            Employee employee = new Employee();
            employee.setEmail(RandomUtil.randomNumbers(6) + "@qq.com");
            employee.setLastName( RandomUtil.randomString("中华人民共国万岁毛主赵钱孙李之初多少钱啊",3) );
            employee.setSalary(new BigDecimal(RandomUtil.randomDouble(3000, 50000, 2, RoundingMode.HALF_UP)));
            employee.setCreateTime(RandomUtil.randomDay(-365 * 5, 365 * 5));
            employee.setUpdateTime(new Date());

            int insert = employeeMapper.insert(employee);
        }
        return rows;
    }

}
